package common.project.epic.db;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import android.content.Context;
import android.content.SharedPreferences;

public class CurrentUser {

	//############################################################################################
	// These settings are related with the activities EpicCreateMsg, EpicSettings: 
	// pay attention in changing these values in order to be coherent through the application!!!
	private static final int INTEREST_AREA_IND_BUILDING = 0;
	private static final int INTEREST_AREA_VAL_BUILDING = 10; // [tens of meters]
	private static final int INTEREST_AREA_IND_QUARTER = 1;
	private static final int INTEREST_AREA_VAL_QUARTER = 100; // [tens of meters]
	private static final int INTEREST_AREA_IND_CITY = 2;
	private static final int INTEREST_AREA_VAL_CITY = 1000; // [tens of meters]
	private static final int INTEREST_AREA_IND_WORLD = 3;
	private static final int INTEREST_AREA_VAL_WORLD = 65000; // [tens of meters]
	
	private static final int INTEREST_PERIOD_IND_WEEK = 0;
	private static final int INTEREST_PERIOD_VAL_WEEK = 60*60*24*7; // [seconds]
	private static final int INTEREST_PERIOD_IND_MONTH = 1;
	private static final int INTEREST_PERIOD_VAL_MONTH = 60*60*24*30; // [seconds]
	private static final int INTEREST_PERIOD_IND_YEAR = 2;
	private static final int INTEREST_PERIOD_VAL_YEAR = 60*60*24*365; // [seconds]
	private static final int INTEREST_PERIOD_IND_INFINITY = 3;
	private static final int INTEREST_PERIOD_VAL_INFINITY = 60*60*24*365*10; // [seconds]
	
	private static final long MIN_QUARANTINE = 20000000;
	private static final long MAX_QUARANTINE = 100000000;
	//############################################################################################
	
	private static final int DEFAULT_GEO_SPAN = INTEREST_AREA_VAL_QUARTER; // [tens of meters]
	private static final int DEFAULT_TIME_SPAN = INTEREST_PERIOD_VAL_WEEK; // [seconds]
	
	// 0 = not checked, 1 = liked, 2 = banned
	private static final String DEFAULT_PREF_CATEGORIES = "0000000000000000";
	// notification = {audio ON/OFF, light ON/OFF, vibration ON/OFF}
	private static final String DEFAULT_NOTIFICATION_TYPE = "100";
	// notification behavior = {always, matching categories, geo span, time span}
	private static final String DEFAULT_NOTIFICATION_BEHAVIOR = "10000";
	// 0 = not checked, 1 = checked
	private static final String DEFAULT_TYPICAL_CATEGORIES = "1000000000000000";
	
	private static final long DEFAULT_QUARANTINE = MIN_QUARANTINE + (MAX_QUARANTINE - MIN_QUARANTINE)/2;
	private static final boolean DEFAULT_BLOW_ON_MIC = false;
	
	private static String currentUsername;
	private static String currentPassword;
	
	private static String name;
	private static String surname;
	
	private static String currentID;
	private static String appID = "vidalebardellazanella";
	private static String currentNickname;
	
	private static float userRate;
	
	private static String prefCategories = DEFAULT_PREF_CATEGORIES;
	private static int interestGeoSpan = DEFAULT_GEO_SPAN;
	private static int interestTimeSpan = DEFAULT_TIME_SPAN;
	
	private static final double signetLat = 45.4088;
	private static final double signetLon = 11.8945;
	
	private static String typicalCategories = DEFAULT_TYPICAL_CATEGORIES;
	
	private static double defaultAltitude = 0;
	private static double defaultLatitude = signetLat;
	private static double defaultLongitude = signetLon;
	private static String defaultPlace = "Padova";
	
	private static int defaultMsgGeoSpan = DEFAULT_GEO_SPAN;
	private static int defaultMsgTimeSpan = DEFAULT_TIME_SPAN;
	
	
	private static String notificationType = DEFAULT_NOTIFICATION_TYPE;
	private static String notificationBehavior = DEFAULT_NOTIFICATION_BEHAVIOR;
	
	private static long quarantineValue = DEFAULT_QUARANTINE;
	
	private static boolean blowOnMic = DEFAULT_BLOW_ON_MIC;
	
	private static SharedPreferences pref;
		
	public static String getCurrentUsername() {
		return currentUsername;
	}
	public static void setCurrentUsername(String currentUsername) {
		CurrentUser.currentUsername = currentUsername;
	}
	public static String getCurrentNickname() {
		return currentNickname;
	}
	public static void setCurrentNickname(String currentNickname) {
		CurrentUser.currentNickname = currentNickname;
	}
	public static String getCurrentPassword() {
		return currentPassword;
	}
	public static void setCurrentPassword(String currentPassword) {
		CurrentUser.currentPassword = currentPassword;
	}
	
	public static float getUserRate() {
		return CurrentUser.userRate;
	}
	public static void setUserRate(float userRate) {
		CurrentUser.userRate = userRate;
	}
	public static String getName() {
		return name;
	}
	public static void setName(String name) {
		CurrentUser.name = name;
	}
	public static String getSurname() {
		return surname;
	}
	public static void setSurname(String surname) {
		CurrentUser.surname = surname;
	}
	
	//##################//
	// USER PREFERENCES //
	//##################//
	// 0 = not checked, 1 = liked, 2 = banned
	public static String getPrefCategories() {
		return prefCategories;
	}
	public static void setPrefCategories(String prefCategories) {
		CurrentUser.prefCategories = prefCategories;
	}
	
	public static long getQuarantineValue() {
		return quarantineValue;
	}
	public static void setQuarantineValue(long quarantineValue) {
		CurrentUser.quarantineValue = quarantineValue;
	}
	
	public static boolean getBlowOnMic() {
		return blowOnMic;
	}
	public static void setBlowOnMic(boolean blowOnMic) {
		CurrentUser.blowOnMic = blowOnMic;
	}
	
	public static int getGeoSpan() {
		return interestGeoSpan;
	}
	public static void setGeoSpan(int interestGeoSpan) {
		CurrentUser.interestGeoSpan = interestGeoSpan;
	}
	
	public static int getTimeSpan() {
		return interestTimeSpan;
	}
	public static void setTimeSpan(int interestTimeSpan) {
		CurrentUser.interestTimeSpan = interestTimeSpan;
	}
	
	// create message default values
	public static String getTypicalCategories() {
		return typicalCategories;
	}
	public static void setTypicalCategories(String typicalCategories) {
		CurrentUser.typicalCategories = typicalCategories;
	}
	
	public static double getDefaultMsgAltitude() {
		return defaultAltitude;
	}
	public static void setDefaultMsgAltitude(double defaultAltitude) {
		CurrentUser.defaultAltitude = defaultAltitude;
	}
	public static double getDefaultMsgLatitude() {
		return defaultLatitude;
	}
	public static void setDefaultMsgLatitude(double defaultLatitude) {
		CurrentUser.defaultLatitude = defaultLatitude;
	}
	public static double getDefaultMsgLongitude() {
		return defaultLongitude;
	}
	public static void setDefaultMsgLongitude(double defaultLongitude) {
		CurrentUser.defaultLongitude = defaultLongitude;
	}
	public static String getDefaultMsgPlace() {
		return defaultPlace;
	}
	public static void setDefaultMsgPlace(String defaultPlace) {
		CurrentUser.defaultPlace = defaultPlace;
	}
	
	public static int getDefaultMsgGeoSpan() {
		return defaultMsgGeoSpan;
	}
	public static void setDefaultMsgGeoSpan(int defaultMsgGeoSpan) {
		CurrentUser.defaultMsgGeoSpan = defaultMsgGeoSpan;
	}
	public static int getDefaultMsgTimeSpan() {
		return defaultMsgTimeSpan;
	}
	public static void setDefaultMsgTimeSpan(int defaultMsgTimeSpan) {
		CurrentUser.defaultMsgTimeSpan = defaultMsgTimeSpan;
	}
	
	
	public static String getNotificationType() {
		return notificationType;
	}
	public static void setNotificationType(String notificationType) {
		CurrentUser.notificationType = notificationType;
	}
	public static String getNotificationBehavior() {
		return notificationBehavior;
	}
	public static void setNotificationBehavior(String notificationBehavior) {
		CurrentUser.notificationBehavior = notificationBehavior;
	}
	
	
	public static void writeCurrentUserSettings(Context c) {
		currentUsername = 
				c.getSharedPreferences("ApplicationPreferencesFile", Context.MODE_PRIVATE)
				.getString("logged_username", "noname");
		currentPassword = 
				c.getSharedPreferences("ApplicationPreferencesFile", Context.MODE_PRIVATE)
				.getString("logged_password", "nopassword");
		
		pref = c.getSharedPreferences(CurrentUser.currentUsername, Context.MODE_PRIVATE);
		pref.edit()
		.putString("currentID", CurrentUser.currentID)
		.putString("currentNickname", CurrentUser.currentNickname)
		.putFloat("userRate", CurrentUser.userRate)
		.putString("name", CurrentUser.name)
		.putString("surname", CurrentUser.surname)
		.putInt("geo_span", CurrentUser.interestGeoSpan)
		.putInt("time_span", CurrentUser.interestTimeSpan)
		.putInt("default_msg_geo_span", CurrentUser.defaultMsgGeoSpan)
		.putInt("default_msg_time_span", CurrentUser.defaultMsgTimeSpan)
		.putFloat("defaultAltitude", (float) CurrentUser.defaultAltitude)
		.putFloat("defaultLatitude", (float) CurrentUser.defaultLatitude)
		.putFloat("defaultLongitude", (float) CurrentUser.defaultLongitude)
		.putString("defaultPlace", CurrentUser.defaultPlace)
		.putString("pref_categories", CurrentUser.prefCategories)
		.putString("notification_type", CurrentUser.notificationType)
		.putString("notification_behavior", CurrentUser.notificationBehavior)
		.putString("typical_categories", CurrentUser.typicalCategories)
		.putLong("quarantineValue", quarantineValue)
		.putBoolean("blowOnMic", blowOnMic)
		.commit();
	}
	
	
	public static void readCurrentUserSettings(Context c) {
		currentUsername = 
				c.getSharedPreferences("ApplicationPreferencesFile", Context.MODE_PRIVATE)
				.getString("logged_username", "noname");
		currentPassword = 
				c.getSharedPreferences("ApplicationPreferencesFile", Context.MODE_PRIVATE)
				.getString("logged_password", "nopassword");
		
		pref = c.getSharedPreferences(CurrentUser.currentUsername, Context.MODE_PRIVATE);
		CurrentUser.currentID = pref.getString("currentID", "defaultValue");
		CurrentUser.currentNickname = pref.getString("currentNickname", "defaultValue");
		CurrentUser.userRate = pref.getFloat("userRate", 3);
		CurrentUser.name = pref.getString("name", "defaultValue");
		CurrentUser.surname = pref.getString("surname", "defaultValue");
		CurrentUser.interestGeoSpan = pref.getInt("geo_span", DEFAULT_GEO_SPAN);
		CurrentUser.interestTimeSpan = pref.getInt("time_span", DEFAULT_TIME_SPAN);
		CurrentUser.defaultMsgGeoSpan = pref.getInt("default_msg_geo_span", DEFAULT_GEO_SPAN);
		CurrentUser.defaultMsgTimeSpan = pref.getInt("default_msg_time_span", DEFAULT_TIME_SPAN);
		CurrentUser.defaultAltitude = (double)pref.getFloat("defaultAltitude", 0);
		CurrentUser.defaultLongitude = (double)pref.getFloat("defaultLongitude", (float)signetLon);
		CurrentUser.defaultLatitude = (double)pref.getFloat("defaultLatitude", (float)signetLat);
		CurrentUser.defaultPlace = pref.getString("defaultPlace", "Padova");
		CurrentUser.prefCategories = pref.getString("pref_categories", DEFAULT_PREF_CATEGORIES);
		CurrentUser.notificationType = pref.getString("notification_type", DEFAULT_NOTIFICATION_TYPE);
		CurrentUser.notificationBehavior = pref.getString("notification_behavior", DEFAULT_NOTIFICATION_BEHAVIOR);
		CurrentUser.typicalCategories = pref.getString("typical_categories", DEFAULT_TYPICAL_CATEGORIES);
		CurrentUser.quarantineValue = pref.getLong("quarantineValue", quarantineValue);
		CurrentUser.blowOnMic = pref.getBoolean("blowOnMic", blowOnMic);
	}
	
	public static String getMd5ID(Context c) {
		readCurrentUserSettings(c);
		CurrentUser.currentID = md5(CurrentUser.currentUsername + CurrentUser.currentPassword + CurrentUser.appID);
		writeCurrentUserSettings(c);
		return CurrentUser.currentID;
	}
	
	//generate md5 digest
	//@params String
	private static String md5(String in) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance("MD5");
            digest.reset();        
            digest.update(in.getBytes());
            byte[] a = digest.digest();
            int len = a.length;
            StringBuilder sb = new StringBuilder(len << 1);
            for (int i = 0; i < len; i++) {
                sb.append(Character.forDigit((a[i] & 0xf0) >> 4, 16));
                sb.append(Character.forDigit(a[i] & 0x0f, 16));
            }
            return sb.toString().substring(0,16);
        } catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
        return null;
    }
	
}
